<?php
/**
 * MVM_MALL 网上商店系统
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * 
 * @author :     www.mvmmall.com <admin@mvmmall.com> 
 * @version :    v4.X
---------------------------------------------
 */
if(!defined('MVMMALL')){
    exit('Access Denied');
}
//插件的代码必须和文件名保持一致
$payment['bill']['name']    = '快钱支付';
//描述
$payment['bill']['desc']    = '快钱支付';
//支付费用
$payment['bill']['pay_fee'] = '1%';
// 是否支持在线支付
$spayment['bill']['online']  = 1;
//否支持货到付款
$payment['bill']['cod']     = 0;
//申请地址
$payment['bill']['reg']     = 'https://www.99bill.com/website/signup/websignup.htm';
//版权信息
$payment['bill']['license']  = '版权所有www.mvmmall.com';
//接口需要的参数
$payment['bill']['cfg'] =array(
        array('name' => 'merchant_id', 'value' => '','label'=>'商户编号'),
        array('name' => 'merchant_key', 'value' => '','label'=>'商户密钥'),
    );
    
class bill
{
    var $cfg;
    
    function  bill($cfg = array())
    {
        foreach ($cfg AS $key=>$val)
        {
            $this->cfg[$val['name']] = $val['value'];
        }
    }
    
/*提交支付请求*/
function pay_send($sn,$amount)	
	{
	    $merchant_id  = $this->cfg[merchant_id];		///商户编号
	    $merchant_key = $this->cfg[merchant_key];		///商户密钥
	    $orderid = $sn;		///订单编号
	    //$amount订单金额
	    $curr = "1";		///货币类型,1为人民币
	    $isSupportDES = "2";		///是否安全校验,2为必校验,推荐
	    $merchant_url = MVMMALL_DIR."/respond.php?code=".basename(__FILE__, '.class.php');;		///支付结果返回地址
	    $pname = urlencode($GLOBALS[m_check_id]);		///支付人姓名
	    $commodity_info = $sn;		///商品信息
	    $merchant_param = "";		///商户私有参数
	    $pemail="";		///传递email到快钱网关页面
	    $pid="";		///代理/合作伙伴商户编号


	    ///生成加密串,注意顺序
	    $ScrtStr="merchant_id=".$merchant_id."&orderid=".$orderid."&amount=".$amount."&merchant_url=".$merchant_url."&merchant_key=".$merchant_key;
	    $mac = strtoupper(md5($ScrtStr));
		$result='
		   <form name="frm" method="post" action="https://www.99bill.com/webapp/receiveMerchantInfoAction.do" target="_blank">
           <input name="merchant_id" type="hidden" value='.$merchant_id.'>
			<input name="orderid"  type="hidden" value='.$orderid.'>
			<input name="amount"  type="hidden" value='.$amount.'>
			<input name="currency"  type="hidden" value='.$curr.'>
			<input name="isSupportDES"  type="hidden" value='.$isSupportDES.'>
			<input name="mac"  type="hidden" value='.$mac.'>

			<input name="merchant_url"  type="hidden"  value='.$merchant_url.'>
			<input name="pname"  type="hidden" value='.$pname.'>
			<input name="commodity_info"  type="hidden"  value='.$commodity_info.'>
			<input name="merchant_param" type="hidden"  value='.$merchant_param.'>
			<input name="pemail" type="hidden"  value='.$pemail.'>
			<input name="pid" type="hidden"  value='.$pid.'>
			<input name="payby99bill"  type="image" src="images/pay/99bil.gif"  value="快钱支付">
           </form>
				';
		$result=trim($result);
		return   $result;
	}
    /**
     * 提交返回处理*
     */
    function pay_receive()
    {
        $merchant_key = $this->cfg[merchant_key];		///商户密钥
        $merchant_id =trim($_REQUEST['merchant_id']);			///获取商户编号
        $orderid = trim($_REQUEST['orderid']);		///获取订单编号
        $amount = trim($_REQUEST['amount']);	///获取订单金额
        $dealdate = trim($_REQUEST['date']);		///获取交易日期
        $succeed = trim($_REQUEST['succeed']);	///获取交易结果,Y成功,N失败
        $mac = trim($_REQUEST['mac']);		///获取安全加密串
        $merchant_param = trim($_REQUEST['merchant_param']);		///获取商户私有参数
        $couponid = trim($_REQUEST['couponid']);		///获取优惠券编码
        $couponvalue = trim($_REQUEST['couponvalue']);		///获取优惠券面额
        ///生成加密串,注意顺序
        $ScrtStr = "merchant_id=".$merchant_id."&orderid=".$orderid."&amount=".$amount."&date=".$dealdate."&succeed=".$succeed."&merchant_key=".$merchant_key;
        $mymac = md5($ScrtStr);
        if(strtoupper($mac)==strtoupper($mymac)){
            if($succeed=="Y"){		///支付成功
                /*
                #商户网站逻辑处理#
                */
                //比较返回的订单号和金额与数据库中的金额是否相符
                $orderid= strip_tags($orderid);
                $list = order_info($orderid);
                if ($list[order_amount]==$amount){
                    /* 改变订单状态*/
                    change_order($orderid);
                    return true;
                }else {
                    //和定单数据库不一致
                    return false;
                }

            }else{
                ///支付失败
                return false;
            }

        }else{
            ///签名错误
            return false;
        }
    }
}